//
//  TicketOrderModel.m
//  ImGuider
//
//  Created by 王鹏宇 on 2018/1/25.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "TicketOrderModel.h"

@implementation TicketOrderModel


- (NSMutableArray *)orderRequestInfoArray {
    
    
    if (!_orderRequestInfoArray) {
        
        _orderRequestInfoArray = [NSMutableArray new];
    }
    [_orderRequestInfoArray removeAllObjects];
    
    for (TicketTypeModel * model in self.ticketDetailModel.ticketTypes) {
        
        switch (model.type.integerValue) {
            case AdultTicket:
            {
                [_orderRequestInfoArray addObject:[self getRecordModelWith:model Array:self.adultArray TicketCount:self.adultTickets]];
            }
                break;
            case ChildrenTicket:
            {
                [_orderRequestInfoArray addObject:[self getRecordModelWith:model Array:self.childArray TicketCount:self.childTickets]];
            }
                break;
            case ElderTicket:
            {
                 [_orderRequestInfoArray addObject:[self getRecordModelWith:model Array:self.elderArray TicketCount:self.elderTickedts]];
                
            }
                break;
            case MemberTicket:
            {
                 [_orderRequestInfoArray addObject:[self getRecordModelWith:model Array:self.memberArray TicketCount:self.memberTickets]];
            }
                break;
            default:
                break;
        }
    }
    
    return _orderRequestInfoArray;
}

- (TicketOrderRecordModel *)getRecordModelWith:(TicketTypeModel *)model Array:(NSArray *)arr  TicketCount:(NSInteger)count{
    
    TicketOrderRecordModel * recordModel = [[TicketOrderRecordModel alloc] init];
    recordModel.tickettype = model.type;
    recordModel.ticketID = model.ID;

    if (model.needdetail) {
        if (arr.count > 0) {
            
            for (TicketMemberInfoModel * memberModel in arr) {
                
                memberModel.ticketPrice = model.smartPirce;
                memberModel.ticketType = model.type;
            }
            [recordModel.persons addObjectsFromArray:arr];
        }
        
    }else{
        
        recordModel.size = [NSString stringWithFormat:@"%ld",count];
    }
    
    
    return recordModel;
}


- (CGFloat)sumMoney {
    
    CGFloat sum = 0;
    
    for (TicketTypeModel * model in self.ticketDetailModel.ticketTypes) {
        
        
        switch (model.type.integerValue) {
            case AdultTicket:
            {
                sum = sum + [self getTheTicketWithTicketModel:model Array:self.adultArray Count:self.adultTickets];
            }
                break;
            case ChildrenTicket:
            {
                sum = sum + [self getTheTicketWithTicketModel:model Array:self.childArray Count:self.childTickets];
            }
                break;
            case ElderTicket:
            {
                sum = sum + [self getTheTicketWithTicketModel:model Array:self.elderArray Count:self.elderTickedts];
            }
                break;
            case MemberTicket:
            {
                sum = sum + [self getTheTicketWithTicketModel:model Array:self.memberArray Count:self.memberTickets];
            }
                break;
            default:
                break;
        }
        
    }
    
    return sum;
}

- (CGFloat)getTheTicketWithTicketModel:(TicketTypeModel *)model Array:(NSArray *)arr Count:(NSInteger)count {
    
    if (model.needdetail) {
        
       return  arr.count * model.price.floatValue;
    }else{
       return  count * model.price.floatValue;
    }
}


- (NSString *)oderSunMoney {
    
    NSString *price = @"";
    if ([[AppManager sharedManager].currencyCode isEqualToString:@"USD"]) {
        
        price = [NSString stringWithFormat:@"$%.2f",[self sumMoney]];
    } else if ([[AppManager sharedManager].currencyCode isEqualToString:@"CNY"]) {
        
        price = [NSString stringWithFormat:@"¥%.2f",[self sumMoney]];
    }
    
    if (price.length == 0) {
        price = [NSString stringWithFormat:@"¥%.2f",[self sumMoney]];
    }
    
    return price;
}
@end
